<?php
/**
 * eg. <{pager pagesum=20}>
 */
function __f1($i, $j, $c) { for ($k = $i; $k <= $j; $k++) $a[] = $c == $k ? "<li class=\"pagebarCurrent\">$k</li>" : "<li><a href=\"?" . __f2($k) . "\">$k</a></li>"; return empty($a) ? '' :  implode(' ', $a); }
function __f2($p) { return preg_match(';(^|&)page=(\d+)($|&);i', $_SERVER["QUERY_STRING"]) ?  preg_replace(';(^|&)page=(\d+)($|&);i', '$1page=' . $p . '$3', $_SERVER["QUERY_STRING"]) : ($_SERVER["QUERY_STRING"] ? $_SERVER["QUERY_STRING"] . '&page=' . $p : 'page=' . $p); }
function smarty_function_pager($params, &$smarty)
{
    ;
    $pagesum = $params['pagesum'];
	if ($pagesum < 2) return '';
    $page = !empty($_GET['page']) ? (int) $_GET['page'] : 1;
    $navleft = $page > 2 ? $page - 2 : 1;
    $navright = $page < $pagesum - 2 ? $page + 2 : $pagesum;
    return '<div class="nav pagebar fix"><ul>'
        . ($page > 1 ? "<li class=\"pagebarPrv\"><a href=\"?" . __f2($page - 1) . "\">" . ($page - 1) . "</a></li>" : '') # pre
        . ' ' . ($navleft > 2 ? __f1(1, 2, $page) : ($navleft == 2 ? __f1(1, 1, $page) : '')) # left
        . ' ' . ($navleft > 3 ? '<li class="pagebarDot">...</li>' : '') # leftdot
        . ' ' . (__f1($navleft, $navright, $page)) # nav
        . ' ' . ($navright < $pagesum - 2 ? '<li class="pagebarDot">...</li>' : '') # rightdot
        . ' ' . ($navright < $pagesum - 1 ? __f1($pagesum - 1, $pagesum, $page) : ($navright == $pagesum - 1 ? __f1($pagesum, $pagesum, $page) : '')) # right
        . ' ' . ($page < $pagesum ? "<li class='pagebarNext'><a href=\"?" . __f2($page + 1) . "\">" . ($page + 1) . "</a></li>" : '') # next
        . '</ul></div>';
}
?>
